package com.monsterPatties.screens 
{
	import com.monsterPatties.interfaces.IDestroyable;
	import com.monsterPatties.utils.displayManager.DMButton;
	import com.monsterPatties.utils.displayManager.event.DisplayManagerEvent;
	import com.monsterPatties.utils.displayManager.Window;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author jc
	 */
	public class PopUpWindow extends Window implements IDestroyable
	{
		/*----------------------------------------------------------Constant----------------------------------------------------*/
		
		/*----------------------------------------------------------Properties----------------------------------------------------*/
		private var _popUpWindowSkin:PopUpWindowMessageSkin;
		private var _displayManagerEvent:DisplayManagerEvent;
		/*----------------------------------------------------------Constrcutor----------------------------------------------------*/
		
		
		public function PopUpWindow( windowName:String, popUpWindowSkin:MovieClip = null ) 
		{
			//_popUpWindowSkin = popUpWindowSkin;
			addEventListener( Event.ADDED_TO_STAGE, init );
			super( windowName );
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			setDisplay();
		}
		
		/*----------------------------------------------------------Methods----------------------------------------------------*/
		private function setDisplay():void 
		{
			_popUpWindowSkin = new PopUpWindowMessageSkin();
			//TODO MUST CREAT BUTTON CLASS THEN IN THAT BUTTON DO THIS LINES BELOW
			var closeBtn:DMButton = new DMButton( "closeBtn", "Close", 1, new CloseBtn() , 127, 192 );
			closeBtn.addEventListener( DisplayManagerEvent.BUTTON_CLICK , onClickCloseBtn );
			_popUpWindowSkin.addEventListener( MouseEvent.MOUSE_DOWN, onStartDrag );
			_popUpWindowSkin.addEventListener( MouseEvent.MOUSE_UP, onStopDrag );
			
			addWindowBg( _popUpWindowSkin );
			_popUpWindowSkin.addChild( closeBtn );			
		}							
		private function removeDisplay():void 
		{
			if ( _popUpWindowSkin != null ) {
				//TODO MUST CREAT BUTTON CLASS THEN IN THAT BUTTON DO THIS LINES BELOW				
				_popUpWindowSkin.removeEventListener( MouseEvent.MOUSE_DOWN, onStartDrag );
				_popUpWindowSkin.removeEventListener( MouseEvent.MOUSE_UP, onStopDrag );
				
				if ( this.contains( _popUpWindowSkin ) ) {
					this.removeChild( _popUpWindowSkin );
					_popUpWindowSkin = null;
				}
			}
		}
		
		public function destroy():void 
		{
			removeDisplay();
		}
		/*----------------------------------------------------------Setters----------------------------------------------------*/
		
		/*----------------------------------------------------------Getters----------------------------------------------------*/
		
		/*----------------------------------------------------------EventHandlers----------------------------------------------------*/
		
		private function onStopDrag(e:MouseEvent):void 
		{
			e.currentTarget.stopDrag();
		}
		
		private function onStartDrag(e:MouseEvent):void 
		{
			e.currentTarget.startDrag();
		}
		
		
		private function onClickCloseBtn( e:DisplayManagerEvent ):void 
		{
			trace( "on close pop up window......" );
			_displayManagerEvent = new DisplayManagerEvent( DisplayManagerEvent.REMOVE_POP_UP_WINDOW );
			_displayManagerEvent.windowName = windowName;
			dispatchEvent( _displayManagerEvent );			
		}
	}

}